import { QuestionCircleOutlined } from '@ant-design/icons';
import { SelectLang, useModel,history } from '@umijs/max';
import { Space ,Select } from 'antd';
import React, { useState,useEffect } from 'react';
import Avatar from './AvatarDropdown';
import styles from './index.less';
import { getStores } from '@/services/erp/api';

export type SiderTheme = 'light' | 'dark';

const { Option } = Select;

const GlobalHeaderRight: React.FC = () => {
  const { initialState,setInitialState } = useModel('@@initialState');
  const [storesList, setStoresList] = useState([]);
  if (!initialState || !initialState.settings) {
    return null;
  }
  const handleChange = (value: string) => {
    console.log(`selected ${value}`);
    localStorage.setItem("currentStoreKey",value)
    for (let index = 0; index < storesList.length; index++) {
      const element = storesList[index];
      if (element.key == value) {
        localStorage.setItem("currentStoreName",element.name)
      }

    }
    setInitialState((s) => ({
      ...s,
      currentStoreKey: value,
    }));
    const urlParams = new URL(window.location.href).searchParams;
    history.push(urlParams.get('redirect') || '/');
  };
  useEffect(() => {
    const fetchData = async () => {
      var params = {
        page: 0,
        size: 10000
      }
      const list = await getStores(params);
      if (!localStorage.getItem("currentStoreKey") && list.data && list.data.length>0) {
        localStorage.setItem("currentStoreKey",list.data[0].key)
        localStorage.setItem("currentStoreName",list.data[0].name)
        setInitialState((s) => ({
          ...s,
          currentStoreKey: list.data[0].key,
        }));
      }
      setStoresList(list.data)
    }
    fetchData().catch(console.error);

  }, [])

  const { navTheme, layout } = initialState.settings;
  let className = styles.right;

  if ((navTheme === 'dark' && layout === 'top') || layout === 'mix') {
    className = `${styles.right}  ${styles.dark}`;
  }

  return (
    <Space className={className}>
      <div>当前门店：
      {storesList.length>0 &&
        <Select defaultValue={initialState.currentStoreKey} style={{ minWidth: 220 }} onChange={handleChange}>
            {storesList.map((option, index) => (
                <Option key={index} value={option.key}>
                  {option.name} | {option.key}
                </Option>
              ))}
        </Select>
      }
      </div>
      {/* <HeaderSearch
        className={`${styles.action} ${styles.search}`}
        placeholder="站内搜索"
        defaultValue="umi ui"
        options={[
          { label: <a href="https://umijs.org/zh/guide/umi-ui.html">umi ui</a>, value: 'umi ui' },
          {
            label: <a href="next.ant.design">Ant Design</a>,
            value: 'Ant Design',
          },
          {
            label: <a href="https://protable.ant.design/">Pro Table</a>,
            value: 'Pro Table',
          },
          {
            label: <a href="https://prolayout.ant.design/">Pro Layout</a>,
            value: 'Pro Layout',
          },
        ]}
        onSearch={value => {
          console.log('input', value);
        }}
      /> */}
      <span
        className={styles.action}
        onClick={() => {
          window.open('http://www.camelee.cn/');
        }}
      >
        <QuestionCircleOutlined />
      </span>
      <Avatar />
      <SelectLang className={styles.action} />
    </Space>
  );
};
export default GlobalHeaderRight;
